<?php

class Model_MessagesMapper
{
    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Model_DbTable_Messages');
        }
        return $this->_dbTable;
    }

    public function findMessageByVenueId($venueId){
    	
    	$messages = array();
    	$select = $this->getDbTable()->select();
    	
    	$select->from('messages')->where('venue_id=?',$venueId)->order(array('public_time DESC'));
    	
    	$messages = $this->findAllBySelector($select);
    	
    	return $messages;
    }
    
    public function findMessageByUserId($userId){
    	
    	$messages = array();
    	$select = $this->getDbTable()->select();
    	
    	$select->from('messages')->where('user_id=?',$userId)->order(array('public_time DESC'));
    	
    	$messages = $this->findAllBySelector($select);
    	
    	return $messages;
    }
    
    public function saveMessage(Model_Messages $message) 
    {
    	$data = array(
        	'message_content' => $message->getMessage_content(),
        	'venue_id' => $message->getVenue_id(),
    		'venue_name' => $message->getVenue_name(),
        	'user_id' => $message->getUser_id(),
    		'user_name' => $message->getUser_name(),
        	'public_time' => $message->getPublic_time(),
    		'reply' => $message->getReply(),
        );

        if (null === ($id = $message->getMessage_id())) {
        	// if no id input, then generate it and insert the data
            $this->getDbTable()->insert($data);
        } else {
        	// if an id input, then update the data which match this id
            $this->getDbTable()->update($data, array('message_id = ?' => $id));
        }
    }
    
    public function findReplyMessage($messageId){
    	
    	$messages = array();
    	$select = $this->getDbTable()->select();
    	$select->from('message')->where('reply=?',$messageId)->order(array('public_time DESC'));
    	
    	$messages = $this->findAllBySelector($select);
    	return $messages;
    }
    
    protected function findAllBySelector($selector) {
    	
    	$results = $this->getDbTable()->fetchAll($selector);
    	
    	if(0 == count($results)){
    		return;
    	}
    	$messages = array();
    	
    	foreach ($results as $row){
    		//$row = $results->current();
    		$message = new Model_Messages();
    		$message->setMessage_id($row->message_id);
    		$message->setVenue_id($row->venue_id);
    		$message->setVenue_name($row->venue_name);
    		$message->setUser_id($row->user_id);
    		$message->setUser_name($row->user_name);
    		$message->setMessage_content($row->message_content);
    		$message->setPublic_time($row->public_time);
    		$message->setReply($row->reply);
    		$messages[] = $message;
    	}
    	
    	return $messages;
    }

    public function find($messageId, Model_Messages $message){
    	
    	$result = $this->getDbTable()->find($messageId);
        
		if (0 == count($result)) {
            return;
        }
        $row = $result->current();
		$message->setMessage_id($row->message_id);
		$message->setMessage_content($row->message_content);
		$message->setPublic_time($row->public_time);
		$message->setReply($row->reply);
		$message->setUser_id($row->user_id);
		$message->setUser_name($row->user_name);
		$message->setVenue_id($row->venue_id);
		$message->setVenue_name($row->venue_name);
    }
}

